
#ifndef __KHAN_VTY_INPUT_H_
#define __KHAN_VTY_INPUT_H_


#include <stdarg.h>     /* for va_start, va_end */
#include <errno.h>
#include <arpa/telnet.h>   
#include <vector>
#include <string>

#include "khan.hpp"
#include "vty_history.hpp"

#include <boost/array.hpp>
#include <boost/circular_buffer.hpp>

class telnet_opt;
typedef boost::shared_ptr<telnet_opt> telnet_opt_ptr;

#define CONTROL(X)  ((X) - '@')

class vty_input
:private boost::noncopyable       
{
public:
    explicit vty_input(vty_ptr vty);
    ~vty_input() {};

    void    start();
    void    stop();
    int     handle_input(std::string& input);
    void    redraw_line();
    void    redraw_line(const std::string& newline);
    void    prompt();
    bool    auth();
    void backward_chars(int size) {
        for (int idx=0; idx < size; idx++) {
            backward_char();
        }
    };


private:
    const std::string AUTH_MODE;
    enum { VTY_NORMAL, VTY_PRE_ESCAPE, VTY_ESCAPE };
    enum { 
        TELNET_BACKWARD_CHAR = 0x08,
        TELNET_SAPCE_CHAR = ' '
    };

    void    prev_line();
    void    next_line();
    void    forward_char();
    void    backward_char();

    void    escape_map(unsigned char c);
    int     handle_vty_more(unsigned char);
    int     handle_vty_escape(unsigned char);
    bool    exec_command();
    bool    self_insert(char c);
    bool    rm_backward_char();

    vty_ptr _vty;   /* FIXME */
    telnet_opt_ptr  _telnet_opt;
    //std::string     _cmd;
    std::string     _banner;
    unsigned char   _escape;
    std::string     _buf;
    size_t          _cp;
    vty_history<string_ptr>     _history;
};

typedef boost::shared_ptr<vty_input> vty_input_ptr;

#endif /* __KHAN_VTY_INPUT_H_ */
